package de.preissler.jointest;

import java.util.List;

import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.beans.SuperRow;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.RangeSuperSlicesQuery;

public class DAXJoin implements DAX
{
	private static final String ISIN = "DE0008469008";

	public void getDAXValues(List<String> dates, Keyspace keyspace, double[] result)
	{
		//Daten zur DAX Berechnung abrufen
		RangeSuperSlicesQuery<String, String, String, Double> query = HFactory.createRangeSuperSlicesQuery(keyspace, SS, SS, SS, DS);
		query.setColumnFamily(SCF);
		query.setKeys(dates.get(dates.size()-1), dates.get(0));
		query.setRange(null, null, false, Integer.MAX_VALUE);
		List<SuperRow<String, String, String, Double>> preResult = query.execute().get().getList();
		
		for(int i = 0; i < preResult.size(); i++)
		{
			double divident = 0.0;
			double divisor = 0.0;
			double kt = 0.0;
			double base = 0.0;
			for(HSuperColumn<String, String, Double> sc : preResult.get(i).getSuperSlice().getSuperColumns())
			{
				if(!sc.getName().equals(ISIN))
				{
					double close = sc.get(2).getValue();
					double pi0 = sc.get(5).getValue();
					if(pi0 == -1.0)
						pi0 = 1.0;
					double qi0 = sc.get(1).getValue();
					if(qi0 == -1.0)
						qi0 = 1.0;
					double qit = sc.get(0).getValue();
					if(qit == -1.0)
						qit = 1.0;
					double ffit = sc.get(4).getValue();
					if(ffit == -1.0)
						ffit = 1.0;
					double ci = sc.get(3).getValue();
					if(ci == -1.0)
						ci = 1.0;
					
					divident = divident + (close*qit*ffit*ci);
					divisor = divisor + (pi0 * qi0);
				}
				else
				{
					kt = sc.get(0).getValue();
					base = sc.get(1).getValue();
				}
			}
			result[preResult.size()-1-i] = kt * (divident/divisor) * base;
		}
	}

}
